﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Tsanie.Common
{
    static class Common
    {
        //public static string GetJson(this Dictionary<string, object> dict)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("{");
        //    foreach (var kv in dict)
        //    {
        //        object o = kv.Value;
        //        if (o is string)
        //        {
        //            sb.AppendFormat("\"{0}\":\"{1}\",", kv.Key, o);
        //        }
        //        else
        //        {
        //            sb.AppendFormat("\"{0}\":{1},", kv.Key, o);
        //        }
        //    }
        //    if (sb.Length > 1)
        //    {
        //        sb.Length--;
        //    }
        //    sb.Append("}");
        //    return sb.ToString();
        //}

#if VALKYRIE_CRUSADE

        public static string GetUserNo(this int user_id, int n)
        {
            string result = null;
            user_id += 1000000;
            while (user_id > 0)
            {
                int i = user_id % n;
                if (i >= 10)
                {
                    result = (char)('a' + i - 10) + result;
                }
                else
                {
                    result = (char)('0' + i) + result;
                }
                user_id /= n;
            }
            if (result == null)
            {
                return "0";
            }
            return result;
        }

#endif

        const long TICKS_19700101 = 621355968000000000;
        const long TICKS_8HOUR = 36000000000 * 8;

        public static string GetTimeSpanFromSeconds(this long seconds)
        {
            long hours = seconds / 3600;
            long minutes = (seconds % 3600) / 60;
            long secs = seconds % 60;
            if (hours > 0)
            {
                return string.Format("{0:n0}:{1:d2}:{2:d2}", hours, minutes, secs);
            }
            return string.Format("{0:n0}:{1:d2}", minutes, secs);
        }

        public static DateTime GetDateTimeFromUnix(this long time, bool ms)
        {
            long ticks = time * 10000L;
            if (!ms)
            {
                // 秒转换成毫秒
                ticks *= 1000;
            }
            return new DateTime(TICKS_19700101 + TICKS_8HOUR + ticks, DateTimeKind.Local);
        }

        public static DateTime GetDateTimeFromUnix(this long time)
        {
            return GetDateTimeFromUnix(time, false);
        }

        public static long GetUnixTime(this DateTime time, bool ms)
        {
            long result = (time.Ticks - TICKS_19700101 - TICKS_8HOUR) / 10000;
            if (!ms)
            {
                // 毫秒转成秒
                result /= 1000;
            }
            return result;
        }

        public static long GetUnixTime(this DateTime time)
        {
            return GetUnixTime(time, false);
        }

        public static string GetHexString(this byte[] data)
        {
            StringBuilder sb = new StringBuilder();
            if (data != null)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    sb.Append(data[i].ToString("x2"));
                }
            }
            return sb.ToString();
        }
    }
}
